package com.zjj.lbw.guava.charMatcher;

import com.google.common.base.CharMatcher;
import org.junit.Test;

/**
 * @author zhanglei.zjj
 * @description guava字符串处理之CharMatcher
 * @date 2023/7/16 16:19
 */
public class TestGuavaCharMatcher {

    /**
     * collapseFrom 配到的字符做替换
     */
    @Test
    public void testCollapseFrom() {
        String input = " Ting Feng ";
        String result =
                CharMatcher.breakingWhitespace().collapseFrom(input, '*');
        System.out.println(result); // *Ting*Feng*
        result = CharMatcher.is(' ').collapseFrom(input, '-');
        System.out.println(result); // -Ting-Feng-
    }

    /**
     * trimFrom 去空格
     * trimLeadingFrom 左边去空格
     * trimTrailingFrom右边去空格
     */
    @Test
    public void testTrim() {
        System.out.println(CharMatcher.breakingWhitespace().trimFrom("  Ting Feng  ")); // Ting Feng
        System.out.println(CharMatcher.breakingWhitespace().trimLeadingFrom("  Ting Feng")); // Ting Feng
        System.out.println(CharMatcher.breakingWhitespace().trimTrailingFrom("Ting Feng  ")); // Ting Feng
    }

    /**
     * is 匹配参数之内的所有字符
     * isNot 匹配参数之外的所有字符
     */
    @Test
    public void testIs_isNot() {
        String input = "a, c, z, 1, 2";
        System.out.println(CharMatcher.is(',').retainFrom(input)); // ,,,,
        System.out.println(CharMatcher.is(',').removeFrom(input)); // a c z 1 2
        System.out.println(CharMatcher.isNot(',').retainFrom(input)); // a c z 1 2
        System.out.println(CharMatcher.isNot(',').removeFrom(input)); // ,,,,
    }

    /**
     * 匹配任意字符
     */
    @Test
    public void testAny() {
        String input = "H*el.lo,}12";
        CharMatcher matcher = CharMatcher.any();
        String result = matcher.retainFrom(input);
        System.out.println(result); // H*el.lo,}12
        matcher = CharMatcher.anyOf("Hel");
        System.out.println(matcher.retainFrom(input)); // Hell
        System.out.println(matcher.removeFrom(input)); // *.o,}12
    }

    /**
     * matchesAllOf 判断sequence所有字符是否都被charMatcher匹配
     * matchesAnyOf 判断sequence中是否存在字符被charMatcher匹配
     * matchesNoneOf 判断sequence所有字符是否都没被charMatcher匹配
     */
    @Test
    public void test_matchesAllOf_matchesAnyOf_matchesNoneOf() {
        String input = "**e,l.lo,}12";
        CharMatcher matcher = CharMatcher.is(',');
        System.out.println(matcher.matchesAllOf(input)); // false
        matcher = CharMatcher.is(',');
        System.out.println(matcher.matchesAnyOf(input)); // true
        matcher = CharMatcher.is('?');
        System.out.println(matcher.matchesNoneOf(input)); // true
    }


}
